﻿using System.IO.Compression;

namespace Datav.Design.Core
{
    /// <summary>
    ///
    /// </summary>
    public class FileExtension
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="paths"></param>
        /// <param name="tempDirectory"></param>
        /// <returns></returns>
        public FileStream Zip(string[] paths, string tempDirectory)
        {
            // 获取应用程序的基目录
            string appDirectory = AppContext.BaseDirectory;
            // 如果目标临时目录存在，先删除
            if (Directory.Exists(tempDirectory))
            {
                Directory.Delete(tempDirectory, true);
            }
            Directory.CreateDirectory(tempDirectory);
            try
            {
                // 遍历所有提供的文件夹和文件路径
                foreach (var path in paths)
                {
                    // 去除路径开头的斜杠，确保它们不会被当作绝对路径
                    string trimmedPath = path.TrimStart('/');

                    // 拼接成完整的文件路径
                    string fullPath = Path.Combine(appDirectory, trimmedPath);
                    //string fullPath = Path.Combine(appDirectory, path);
                    if (Directory.Exists(fullPath))
                    {
                        // 如果路径是文件夹，复制文件夹
                        CopyDirectory(fullPath, tempDirectory);
                    }
                    else if (File.Exists(fullPath))
                    {
                        // 如果路径是文件，复制文件
                        string destFilePath = Path.Combine(tempDirectory, Path.GetFileName(fullPath));
                        File.Copy(fullPath, destFilePath, overwrite: true);
                    }
                    else
                    {
                        // 如果路径既不是文件夹也不是文件，返回错误
                        //return BadRequest($"Path not found: {fullPath}");
                    }
                }

                // 设置 ZIP 文件路径
                var zipFileName = $"merged_{Guid.NewGuid()}.zip";
                var zipFilePath = Path.Combine(Path.GetTempPath(), zipFileName);
                // 压缩临时目录
                ZipFile.CreateFromDirectory(tempDirectory, zipFilePath);
                // 删除临时目录
                Directory.Delete(tempDirectory, true);
                // 返回 ZIP 文件供下载
                var zipFileStream = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read);
                return zipFileStream;
                //return File(zipFileStream, "application/zip", zipFileName);
            }
            catch (Exception ex)
            {
                //return StatusCode(500, $"Error during compression: {ex.Message}");
            }
            return null;
        }

        /// <summary>
        ///  将目录复制到目标路径
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="destDir"></param>
        private void CopyDirectory(string sourceDir, string destDir)
        {
            DirectoryInfo dir = new DirectoryInfo(sourceDir);
            // 创建目标目录
            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }
            // 复制文件
            foreach (FileInfo file in dir.GetFiles())
            {
                string tempPath = Path.Combine(destDir, file.Name);
                file.CopyTo(tempPath, overwrite: true);
            }
            // 递归复制子目录
            foreach (DirectoryInfo subDir in dir.GetDirectories())
            {
                string tempPath = Path.Combine(destDir, subDir.Name);
                CopyDirectory(subDir.FullName, tempPath);
            }
        }
    }
}